package com.gxa.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.base.RespResult;
import com.gxa.service.dto.SpecificationAreaDto;
import com.gxa.service.entity.Specification;
import com.gxa.service.entity.SpecificationArea;
import com.gxa.service.mapper.SpecificationMapper;
import com.gxa.service.service.SpecificationAreaService;
import com.gxa.service.mapper.SpecificationAreaMapper;
import com.gxa.service.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
public class SpecificationAreaServiceImpl extends ServiceImpl<SpecificationAreaMapper, SpecificationArea>
    implements SpecificationAreaService{
    @Autowired
    private SpecificationAreaMapper specificationAreaMapper;
    @Autowired
    private SpecificationMapper specificationMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RespResult addSpecification(SpecificationAreaDto specificationAreas) {
        //给对应的set服务添加定价信息
        Specification specification=new Specification();
        specification.setSpecificationName(specificationAreas.getSpecificationName());
        specification.setSetServiceId(specificationAreas.getSetServiceId());
        System.out.println(specification);
        String name=specification.getSpecificationName();
        Integer setId=specification.getSetServiceId();
        int row1=specificationMapper.addSpecification(name,setId);
        Integer id=specificationMapper.findSpecificationId(name,setId);
//      先删除已有的
        int row=specificationAreaMapper.deleteByID(id);
//        添加规格面积
        for(int i=0;i<specificationAreas.getAreas().size();i++){
            SpecificationArea specificationArea=specificationAreas.getAreas().get(i);
            specificationArea.setSpecificationId(id);
            specificationAreaMapper.insert(specificationArea);
        }

        return RespResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RespResult updateSpecification(SpecificationAreaDto specificationAreas) {
        //先删除已有的
        specificationAreaMapper.deleteByID(specificationAreas.getSpecificationId());
//        添加规格面积
        for(int i=0;i<specificationAreas.getAreas().size();i++){
            SpecificationArea specificationArea=specificationAreas.getAreas().get(i);
            specificationArea.setSpecificationId(specificationAreas.getSpecificationId());
            specificationAreaMapper.insert(specificationArea);
        }
        return RespResult.success();
    }
}




